package com.imooc.reader.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imooc.reader.portal.dto.ResponseDTO;
import com.imooc.reader.portal.entity.Book;
import com.imooc.reader.portal.service.BookService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/books")
public class BookController {
    @Resource
    private BookService bookService;
    @GetMapping("")
    public ResponseDTO list(Long categoryId , String order , Integer page){
        IPage<Book> bookIPage = bookService.selectPage(categoryId, order, page, 10);
        return new ResponseDTO().put("page", bookIPage);
    }
    @GetMapping("/{id}")
    public ResponseDTO selectById(@PathVariable("id") Long id){
        Book book = bookService.selectById(id);
        return new ResponseDTO().put("book", book);
    }
    @GetMapping("/set")
    public String setSession(HttpSession session) {
        session.setAttribute("username", "user123");
        return "Session data set";
    }
}
